package com.example.roadeyeserver.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.roadeyeserver.constant.ProjectConstant;
import com.example.roadeyeserver.entity.StationHourFlowWide;
import com.example.roadeyeserver.entity.StationRoadAreaInfo;
import com.example.roadeyeserver.entity.TobjTollStationInfo;
import com.example.roadeyeserver.entity.report.CrossOverviewReport;
import com.example.roadeyeserver.entity.report.CrossStationStatisticReport;
import com.example.roadeyeserver.entity.report.StationRankReport;
import com.example.roadeyeserver.entity.request.CommonRequest;
import com.example.roadeyeserver.entity.request.CrossStationStatisticRequest;
import com.example.roadeyeserver.entity.vo.GantryHourFlowWideVO;
import com.example.roadeyeserver.entity.vo.StationHourFlowWideVO;
import com.example.roadeyeserver.enums.CarTypeEnum;
import com.example.roadeyeserver.mapper.StationHourFlowWideMapper;
import com.example.roadeyeserver.result.Result;
import com.example.roadeyeserver.service.StationHourFlowWideService;
import com.example.roadeyeserver.service.StationRoadAreaInfoService;
import com.example.roadeyeserver.service.TobjTollStationInfoService;
import com.example.roadeyeserver.util.DateTimeUtil;
import com.example.roadeyeserver.util.QueryUtil;
import com.example.roadeyeserver.util.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
@Slf4j
public class StationHourFlowWideServiceImpl extends ServiceImpl<StationHourFlowWideMapper, StationHourFlowWide> implements StationHourFlowWideService {

    @Resource
    private StationHourFlowWideMapper stationHourFlowWideMapper;

    @Resource
    private TobjTollStationInfoService tollStationInfoService;

    @Resource
    private StationRoadAreaInfoService stationRoadAreaInfoService;

    @Override
    public Result<CrossOverviewReport> getStationOverviewReport(CommonRequest request) {
        String isValid = QueryUtil.isValid(request);
        // 判断请求是否合法
        if (!ProjectConstant.TRUE.equals(isValid)) {
            return ResultUtil.fail(isValid);
        }
        // 获取所有信息
        List<StationRoadAreaInfo> stationRoadAreaInfos = stationRoadAreaInfoService.list();
        // 获取道路编码-收费站id映射
        Map<String, List<String>> roadMap = stationRoadAreaInfos.stream()
                .collect(Collectors.groupingBy(StationRoadAreaInfo::getRoadCode, Collectors.mapping(StationRoadAreaInfo::getStationId, Collectors.toList())));
        // 获取区域-收费站id映射
        Map<String, List<String>> areaMap = stationRoadAreaInfos.stream()
                .collect(Collectors.groupingBy(StationRoadAreaInfo::getAreaName, Collectors.mapping(StationRoadAreaInfo::getStationId, Collectors.toList())));
        // 道路
        String road = request.getRoad();
        if (StringUtils.isNotEmpty(road)) {
            List<String> roadStationIds = roadMap.get(road);
            request.setRoadStationIds(roadStationIds);
        }
        // 区域
        String area = request.getArea();
        if (StringUtils.isNotEmpty(area)) {
            List<String> areaStationIds = areaMap.get(area);
            request.setAreaStationIds(areaStationIds);
        }
        // 按要求查询数据
        StationHourFlowWideVO stationHourFlowWideVO = stationHourFlowWideMapper.getStationOverviewReport(request);
        if (stationHourFlowWideVO == null) {
            return ResultUtil.success(new CrossOverviewReport());
        }
        CrossOverviewReport report = new CrossOverviewReport();
        // 总流量
        Integer totalFlow =
                stationHourFlowWideVO.getVhc1() +
                stationHourFlowWideVO.getVhc2() +
                stationHourFlowWideVO.getVhc3() +
                stationHourFlowWideVO.getVhc4() +
                stationHourFlowWideVO.getVhc11() +
                stationHourFlowWideVO.getVhc12() +
                stationHourFlowWideVO.getVhc13() +
                stationHourFlowWideVO.getVhc14() +
                stationHourFlowWideVO.getVhc15() +
                stationHourFlowWideVO.getVhc16();
        // 客车流量
        Integer busFlow =
                stationHourFlowWideVO.getVhc1() +
                stationHourFlowWideVO.getVhc2() +
                stationHourFlowWideVO.getVhc3() +
                stationHourFlowWideVO.getVhc4();
        // 货车流量
        Integer truckFlow =
                stationHourFlowWideVO.getVhc11() +
                stationHourFlowWideVO.getVhc12() +
                stationHourFlowWideVO.getVhc13() +
                stationHourFlowWideVO.getVhc14() +
                stationHourFlowWideVO.getVhc15() +
                stationHourFlowWideVO.getVhc16();
        report.setTotalFlow(totalFlow);
        report.setTruckFlow(truckFlow);
        report.setBusFlow(busFlow);
        // 下述字段用于单独筛选某种车型的时候展示，让前端自主判断
        report.setVhc1(stationHourFlowWideVO.getVhc1());
        report.setVhc2(stationHourFlowWideVO.getVhc2());
        report.setVhc3(stationHourFlowWideVO.getVhc3());
        report.setVhc4(stationHourFlowWideVO.getVhc4());
        report.setVhc11(stationHourFlowWideVO.getVhc11());
        report.setVhc12(stationHourFlowWideVO.getVhc12());
        report.setVhc13(stationHourFlowWideVO.getVhc13());
        report.setVhc14(stationHourFlowWideVO.getVhc14());
        report.setVhc15(stationHourFlowWideVO.getVhc15());
        report.setVhc16(stationHourFlowWideVO.getVhc16());
        report.setVhc21(stationHourFlowWideVO.getVhc21());
        report.setVhc22(stationHourFlowWideVO.getVhc22());
        report.setVhc23(stationHourFlowWideVO.getVhc23());
        report.setVhc24(stationHourFlowWideVO.getVhc24());
        report.setVhc25(stationHourFlowWideVO.getVhc25());
        report.setVhc26(stationHourFlowWideVO.getVhc26());
        // 车型选择
        String carTypeChoose = request.getCarTypeChoose();
        // 如果对车型没有选择,直接返回
        if (StringUtils.isEmpty(carTypeChoose)) {
            return ResultUtil.success(report);
        }
        CarTypeEnum.calculateAndSetObjectFlow(carTypeChoose, report);
        return ResultUtil.success(report);
    }

    @Override
    public Result<List<StationRankReport>> getStationRankReport(CommonRequest request) {
        String isValid = QueryUtil.isValid(request);
        // 判断请求是否合法
        if (!ProjectConstant.TRUE.equals(isValid)) {
            return ResultUtil.fail(isValid);
        }
        // 收费站id-name的map
        Map<String, String> stationMap = tollStationInfoService.list().stream().collect(
                Collectors.toMap(TobjTollStationInfo::getStationId, TobjTollStationInfo::getStationName)
        );
        // 获取所有信息
        List<StationRoadAreaInfo> stationRoadAreaInfos = stationRoadAreaInfoService.list();
        // 获取道路编码-收费站id映射
        Map<String, List<String>> roadMap = stationRoadAreaInfos.stream()
                .collect(Collectors.groupingBy(StationRoadAreaInfo::getRoadCode, Collectors.mapping(StationRoadAreaInfo::getStationId, Collectors.toList())));
        // 获取区域-收费站id映射
        Map<String, List<String>> areaMap = stationRoadAreaInfos.stream()
                .collect(Collectors.groupingBy(StationRoadAreaInfo::getAreaName, Collectors.mapping(StationRoadAreaInfo::getStationId, Collectors.toList())));
        // 道路
        String road = request.getRoad();
        if (StringUtils.isNotEmpty(road)) {
            List<String> roadStationIds = roadMap.get(road);
            request.setRoadStationIds(roadStationIds);
        }
        // 区域
        String area = request.getArea();
        if (StringUtils.isNotEmpty(area)) {
            List<String> areaStationIds = areaMap.get(area);
            request.setAreaStationIds(areaStationIds);
        }
        List<StationHourFlowWideVO> records = stationHourFlowWideMapper.getStationRankReport(request);
        List<StationRankReport> result = new ArrayList<>();
        for (StationHourFlowWideVO record : records) {
            StationRankReport report = new StationRankReport();
            report.setStationId(record.getStationId());
            report.setStationName(stationMap.get(record.getStationId()));
            report.setTotalFlow(
                    record.getVhc1() +
                    record.getVhc2() +
                    record.getVhc3() +
                    record.getVhc4() +
                    record.getVhc11() +
                    record.getVhc12() +
                    record.getVhc13() +
                    record.getVhc14() +
                    record.getVhc15() +
                    record.getVhc16());
            report.setBusFlow(
                    record.getVhc1() +
                    record.getVhc2() +
                    record.getVhc3() +
                    record.getVhc4());
            report.setTruckFlow(
                    record.getVhc11() +
                    record.getVhc12() +
                    record.getVhc13() +
                    record.getVhc14() +
                    record.getVhc15() +
                    record.getVhc16());
            // 下述字段用于单独筛选某种车型的时候展示，让前端自主判断
            report.setVhc1(record.getVhc1());
            report.setVhc2(record.getVhc2());
            report.setVhc3(record.getVhc3());
            report.setVhc4(record.getVhc4());
            report.setVhc11(record.getVhc11());
            report.setVhc12(record.getVhc12());
            report.setVhc13(record.getVhc13());
            report.setVhc14(record.getVhc14());
            report.setVhc15(record.getVhc15());
            report.setVhc16(record.getVhc16());
            report.setVhc21(record.getVhc21());
            report.setVhc22(record.getVhc22());
            report.setVhc23(record.getVhc23());
            report.setVhc24(record.getVhc24());
            report.setVhc25(record.getVhc25());
            report.setVhc26(record.getVhc26());
            result.add(report);
        }
        // 对reports按照总流量降序进行排序
        result.sort(Comparator.comparingInt(StationRankReport::getTotalFlow).reversed());
        // 车型选择
        String carTypeChoose = request.getCarTypeChoose();
        // 如果对车型没有选择,直接返回
        if (StringUtils.isEmpty(carTypeChoose)) {
            return ResultUtil.success(result);
        }
        CarTypeEnum.calculateAndSetListFlow(carTypeChoose, result);
        return ResultUtil.success(result);
    }

    @Override
    public Result<List<CrossStationStatisticReport>> getCrossStationStatisticReport(CrossStationStatisticRequest request) {
        String isValid = QueryUtil.isValid(request);
        // 判断请求是否合法
        if (!ProjectConstant.TRUE.equals(isValid)) {
            return ResultUtil.fail(isValid);
        }
        // 收费站id不能为空
        String stationId = request.getStationId();
        if (StringUtils.isEmpty(stationId)) {
            return ResultUtil.fail("收费站id不能为空!");
        }
        // 统计类型不能为空
        String statisticType = request.getStatisticType();
        if (StringUtils.isEmpty(statisticType)) {
            return ResultUtil.fail("统计类型不能为空!");
        }
        // 获取所有信息
        List<StationRoadAreaInfo> stationRoadAreaInfos = stationRoadAreaInfoService.list();
        // 获取道路编码-收费站id映射
        Map<String, List<String>> roadMap = stationRoadAreaInfos.stream()
                .collect(Collectors.groupingBy(StationRoadAreaInfo::getRoadCode, Collectors.mapping(StationRoadAreaInfo::getStationId, Collectors.toList())));
        // 获取区域-收费站id映射
        Map<String, List<String>> areaMap = stationRoadAreaInfos.stream()
                .collect(Collectors.groupingBy(StationRoadAreaInfo::getAreaName, Collectors.mapping(StationRoadAreaInfo::getStationId, Collectors.toList())));
        // 道路
        String road = request.getRoad();
        if (StringUtils.isNotEmpty(road)) {
            List<String> roadStationIds = roadMap.get(road);
            request.setRoadStationIds(roadStationIds);
        }
        // 区域
        String area = request.getArea();
        if (StringUtils.isNotEmpty(area)) {
            List<String> areaStationIds = areaMap.get(area);
            request.setAreaStationIds(areaStationIds);
        }
        // 返回列表
        List<CrossStationStatisticReport> result = new ArrayList<>();
        if (ProjectConstant.DIRECTION.equals(statisticType)) {
            // 根据收费站id查询数据
            List<StationHourFlowWideVO> records = stationHourFlowWideMapper.getFlow(request);
            // 根据时间类型区分,填写所属时间点并在这个时间点内做累加
            String timeType = request.getTimeType();
            // 根据开始时间、结束时间、时间类型生成时间点列表
            List<String> timePointList = DateTimeUtil.generateTimeList(request.getStartTime(), request.getEndTime(), timeType);
            // 遍历时间点列表并获取对应的数据
            for (int i = 0; i < timePointList.size() - 1; i++) {
                CrossStationStatisticReport report = new CrossStationStatisticReport();
                // 当前时间点
                String timePoint = timePointList.get(i);
                Date timePointDate = DateUtil.parse(timePoint);
                // 下一个时间点
                String nextTimePoint = timePointList.get(i + 1);
                Date nextTimePointDate = DateUtil.parse(nextTimePoint);
                // 时间点
                String timePointDisplay = DateTimeUtil.getTimePoint(timePoint, timeType);
                report.setTimePoint(timePointDisplay);
                // 这个时间段内的入口流量数据(大于等于timePoint,小于nextTimePoint)
                int inFlow = 0;
                int outFlow = 0;
                if (CollectionUtil.isNotEmpty(records)) {
                    // 入口总流量
                    List<StationHourFlowWideVO> inFlowList = records.stream()
                            .filter(item -> item.getTime() != null && item.getTime().compareTo(timePointDate) >= 0 && item.getTime().compareTo(nextTimePointDate) < 0 && item.getInOut() != null && item.getInOut() == 1)
                            .collect(Collectors.toList());
                    inFlow = inFlowList.stream()
                            .mapToInt(item ->
                                    item.getVhc1() + item.getVhc2() + item.getVhc3() + item.getVhc4() +
                                    item.getVhc11() + item.getVhc12() + item.getVhc13() + item.getVhc14() + item.getVhc15() + item.getVhc16())
                            .sum();
                    // 下述字段用于单独筛选某种车型的时候展示，让前端自主判断
                    report.setInVhc1(inFlowList.stream().mapToInt(StationHourFlowWideVO::getVhc1).sum());
                    report.setInVhc2(inFlowList.stream().mapToInt(StationHourFlowWideVO::getVhc2).sum());
                    report.setInVhc3(inFlowList.stream().mapToInt(StationHourFlowWideVO::getVhc3).sum());
                    report.setInVhc4(inFlowList.stream().mapToInt(StationHourFlowWideVO::getVhc4).sum());
                    report.setInVhc11(inFlowList.stream().mapToInt(StationHourFlowWideVO::getVhc11).sum());
                    report.setInVhc12(inFlowList.stream().mapToInt(StationHourFlowWideVO::getVhc12).sum());
                    report.setInVhc13(inFlowList.stream().mapToInt(StationHourFlowWideVO::getVhc13).sum());
                    report.setInVhc14(inFlowList.stream().mapToInt(StationHourFlowWideVO::getVhc14).sum());
                    report.setInVhc15(inFlowList.stream().mapToInt(StationHourFlowWideVO::getVhc15).sum());
                    report.setInVhc16(inFlowList.stream().mapToInt(StationHourFlowWideVO::getVhc16).sum());
                    report.setInVhc21(inFlowList.stream().mapToInt(StationHourFlowWideVO::getVhc21).sum());
                    report.setInVhc22(inFlowList.stream().mapToInt(StationHourFlowWideVO::getVhc22).sum());
                    report.setInVhc23(inFlowList.stream().mapToInt(StationHourFlowWideVO::getVhc23).sum());
                    report.setInVhc24(inFlowList.stream().mapToInt(StationHourFlowWideVO::getVhc24).sum());
                    report.setInVhc25(inFlowList.stream().mapToInt(StationHourFlowWideVO::getVhc25).sum());
                    report.setInVhc26(inFlowList.stream().mapToInt(StationHourFlowWideVO::getVhc26).sum());
                    // 出口总流量
                    List<StationHourFlowWideVO> outFlowList = records.stream()
                            .filter(item -> item.getTime() != null &&  item.getTime().compareTo(timePointDate) >= 0 && item.getTime().compareTo(nextTimePointDate) < 0 && item.getInOut() != null && item.getInOut() == 2)
                            .collect(Collectors.toList());
                    outFlow = outFlowList.stream()
                            .mapToInt(item ->
                                    item.getVhc1() + item.getVhc2() + item.getVhc3() + item.getVhc4() +
                                    item.getVhc11() + item.getVhc12() + item.getVhc13() + item.getVhc14() + item.getVhc15() + item.getVhc16())
                            .sum();
                    // 下述字段用于单独筛选某种车型的时候展示，让前端自主判断
                    report.setOutVhc1(outFlowList.stream().mapToInt(StationHourFlowWideVO::getVhc1).sum());
                    report.setOutVhc2(outFlowList.stream().mapToInt(StationHourFlowWideVO::getVhc2).sum());
                    report.setOutVhc3(outFlowList.stream().mapToInt(StationHourFlowWideVO::getVhc3).sum());
                    report.setOutVhc4(outFlowList.stream().mapToInt(StationHourFlowWideVO::getVhc4).sum());
                    report.setOutVhc11(outFlowList.stream().mapToInt(StationHourFlowWideVO::getVhc11).sum());
                    report.setOutVhc12(outFlowList.stream().mapToInt(StationHourFlowWideVO::getVhc12).sum());
                    report.setOutVhc13(outFlowList.stream().mapToInt(StationHourFlowWideVO::getVhc13).sum());
                    report.setOutVhc14(outFlowList.stream().mapToInt(StationHourFlowWideVO::getVhc14).sum());
                    report.setOutVhc15(outFlowList.stream().mapToInt(StationHourFlowWideVO::getVhc15).sum());
                    report.setOutVhc16(outFlowList.stream().mapToInt(StationHourFlowWideVO::getVhc16).sum());
                    report.setOutVhc21(outFlowList.stream().mapToInt(StationHourFlowWideVO::getVhc21).sum());
                    report.setOutVhc22(outFlowList.stream().mapToInt(StationHourFlowWideVO::getVhc22).sum());
                    report.setOutVhc23(outFlowList.stream().mapToInt(StationHourFlowWideVO::getVhc23).sum());
                    report.setOutVhc24(outFlowList.stream().mapToInt(StationHourFlowWideVO::getVhc24).sum());
                    report.setOutVhc25(outFlowList.stream().mapToInt(StationHourFlowWideVO::getVhc25).sum());
                    report.setOutVhc26(outFlowList.stream().mapToInt(StationHourFlowWideVO::getVhc26).sum());
                }
                // 总流量
                report.setTotalFlow(inFlow + outFlow);
                report.setInFlow(inFlow);
                report.setOutFlow(outFlow);
                result.add(report);
            }
        } else if (ProjectConstant.CAR_TYPE.equals(statisticType)) {
            // 根据收费站id查询数据
            List<StationHourFlowWideVO> records = stationHourFlowWideMapper.getFlow(request);
            // 根据时间类型区分,填写所属时间点并在这个时间点内做累加
            String timeType = request.getTimeType();
            // 根据开始时间、结束时间、时间类型生成时间点列表
            List<String> timePointList = DateTimeUtil.generateTimeList(request.getStartTime(), request.getEndTime(), timeType);
            // 遍历时间点列表并获取对应的数据
            for (int i = 0; i < timePointList.size() - 1; i++) {
                CrossStationStatisticReport report = new CrossStationStatisticReport();
                // 当前时间点
                String timePoint = timePointList.get(i);
                Date timePointDate = DateUtil.parse(timePoint);
                // 下一个时间点
                String nextTimePoint = timePointList.get(i + 1);
                Date nextTimePointDate = DateUtil.parse(nextTimePoint);
                // 时间点
                String timePointDisplay = DateTimeUtil.getTimePoint(timePoint, timeType);
                report.setTimePoint(timePointDisplay);
                int busFlow = 0;
                int truckFlow = 0;
                int containerFlow = 0;
                // 这个时间段内的流量数据(大于等于timePoint,小于nextTimePoint)
                if (CollectionUtil.isNotEmpty(records)) {
                    List<StationHourFlowWideVO> flowList = records.stream()
                            .filter(item -> item.getTime() != null &&  item.getTime().compareTo(timePointDate) >= 0 && item.getTime().compareTo(nextTimePointDate) < 0)
                            .collect(Collectors.toList());
                    // 客车总流量
                    busFlow = flowList.stream()
                            .mapToInt(item -> item.getVhc1() + item.getVhc2() + item.getVhc3() + item.getVhc4()).sum();
                    // 货车总流量
                    truckFlow = flowList.stream()
                            .mapToInt(item -> item.getVhc11() + item.getVhc12() + item.getVhc13() + item.getVhc14() + item.getVhc15() + item.getVhc16()).sum();
                    // 下述字段用于单独筛选某种车型的时候展示，让前端自主判断
                    report.setVhc1(flowList.stream().mapToInt(StationHourFlowWideVO::getVhc1).sum());
                    report.setVhc2(flowList.stream().mapToInt(StationHourFlowWideVO::getVhc2).sum());
                    report.setVhc3(flowList.stream().mapToInt(StationHourFlowWideVO::getVhc3).sum());
                    report.setVhc4(flowList.stream().mapToInt(StationHourFlowWideVO::getVhc4).sum());
                    report.setVhc11(flowList.stream().mapToInt(StationHourFlowWideVO::getVhc11).sum());
                    report.setVhc12(flowList.stream().mapToInt(StationHourFlowWideVO::getVhc12).sum());
                    report.setVhc13(flowList.stream().mapToInt(StationHourFlowWideVO::getVhc13).sum());
                    report.setVhc14(flowList.stream().mapToInt(StationHourFlowWideVO::getVhc14).sum());
                    report.setVhc15(flowList.stream().mapToInt(StationHourFlowWideVO::getVhc15).sum());
                    report.setVhc16(flowList.stream().mapToInt(StationHourFlowWideVO::getVhc16).sum());
                    report.setVhc21(flowList.stream().mapToInt(StationHourFlowWideVO::getVhc21).sum());
                    report.setVhc22(flowList.stream().mapToInt(StationHourFlowWideVO::getVhc22).sum());
                    report.setVhc23(flowList.stream().mapToInt(StationHourFlowWideVO::getVhc23).sum());
                    report.setVhc24(flowList.stream().mapToInt(StationHourFlowWideVO::getVhc24).sum());
                    report.setVhc25(flowList.stream().mapToInt(StationHourFlowWideVO::getVhc25).sum());
                    report.setVhc26(flowList.stream().mapToInt(StationHourFlowWideVO::getVhc26).sum());
                }
                // 总流量
                report.setTotalFlow(busFlow + truckFlow + containerFlow);
                report.setBusFlow(busFlow);
                report.setTruckFlow(truckFlow);
                report.setContainerFlow(containerFlow);
                result.add(report);
            }
        }
        // 车型选择
        String carTypeChoose = request.getCarTypeChoose();
        // 如果对车型没有选择,直接返回
        if (StringUtils.isEmpty(carTypeChoose)) {
            return ResultUtil.success(result);
        }
        CarTypeEnum.calculateAndSetObjectFlowInOutListFlow(carTypeChoose, result);
        return ResultUtil.success(result);
    }

    @Override
    public Result<StationHourFlowWideVO> getStationHourFlowList(CrossStationStatisticRequest request) {
        String isValid = QueryUtil.isValid(request);
        // 判断请求是否合法
        if (!ProjectConstant.TRUE.equals(isValid)) {
            return ResultUtil.fail(isValid);
        }
        // 收费站id不能为空
        if (StringUtils.isEmpty(request.getStationId())) {
            return ResultUtil.fail("收费站id不能为空");
        }
        // 获取所有信息
        List<StationRoadAreaInfo> stationRoadAreaInfos = stationRoadAreaInfoService.list();
        // 获取道路编码-收费站id映射
        Map<String, List<String>> roadMap = stationRoadAreaInfos.stream()
                .collect(Collectors.groupingBy(StationRoadAreaInfo::getRoadCode, Collectors.mapping(StationRoadAreaInfo::getStationId, Collectors.toList())));
        // 获取区域-收费站id映射
        Map<String, List<String>> areaMap = stationRoadAreaInfos.stream()
                .collect(Collectors.groupingBy(StationRoadAreaInfo::getAreaName, Collectors.mapping(StationRoadAreaInfo::getStationId, Collectors.toList())));
        // 道路
        String road = request.getRoad();
        if (StringUtils.isNotEmpty(road)) {
            List<String> roadStationIds = roadMap.get(road);
            request.setRoadStationIds(roadStationIds);
        }
        // 区域
        String area = request.getArea();
        if (StringUtils.isNotEmpty(area)) {
            List<String> areaStationIds = areaMap.get(area);
            request.setAreaStationIds(areaStationIds);
        }
        StationHourFlowWideVO records = stationHourFlowWideMapper.getStationOverviewReport(request);
        // 车型选择
        String carTypeChoose = request.getCarTypeChoose();
        // 如果对车型没有选择,直接返回
        if (StringUtils.isEmpty(carTypeChoose)) {
            return ResultUtil.success(records);
        }
        CarTypeEnum.setCarTypeStationObjectFLow(carTypeChoose, records);
        return ResultUtil.success(records);
    }

    @Override
    public Result<List<StationHourFlowWideVO>> getStationCarReport(CrossStationStatisticRequest request) {
        String isValid = QueryUtil.isValid(request);
        // 判断请求是否合法
        if (!ProjectConstant.TRUE.equals(isValid)) {
            return ResultUtil.fail(isValid);
        }
        // 收费站id不能为空
        if (StringUtils.isEmpty(request.getStationId())) {
            return ResultUtil.fail("收费站id不能为空");
        }
        // 获取所有信息
        List<StationRoadAreaInfo> stationRoadAreaInfos = stationRoadAreaInfoService.list();
        // 获取道路编码-收费站id映射
        Map<String, List<String>> roadMap = stationRoadAreaInfos.stream()
                .collect(Collectors.groupingBy(StationRoadAreaInfo::getRoadCode, Collectors.mapping(StationRoadAreaInfo::getStationId, Collectors.toList())));
        // 获取区域-收费站id映射
        Map<String, List<String>> areaMap = stationRoadAreaInfos.stream()
                .collect(Collectors.groupingBy(StationRoadAreaInfo::getAreaName, Collectors.mapping(StationRoadAreaInfo::getStationId, Collectors.toList())));
        // 道路
        String road = request.getRoad();
        if (StringUtils.isNotEmpty(road)) {
            List<String> roadStationIds = roadMap.get(road);
            request.setRoadStationIds(roadStationIds);
        }
        // 区域
        String area = request.getArea();
        if (StringUtils.isNotEmpty(area)) {
            List<String> areaStationIds = areaMap.get(area);
            request.setAreaStationIds(areaStationIds);
        }
        // 查询数据
        List<StationHourFlowWideVO> records = stationHourFlowWideMapper.getFlow(request);
        // 根据时间类型区分,填写所属时间点并在这个时间点内做累加
        String timeType = request.getTimeType();
        // 根据开始时间、结束时间、时间类型生成时间点列表
        List<String> timePointList = DateTimeUtil.generateTimeList(request.getStartTime(), request.getEndTime(), timeType);
        List<StationHourFlowWideVO> result = new ArrayList<>();
        // 遍历时间点列表并获取对应的数据
        for (int i = 0; i < timePointList.size() - 1; i++) {
            StationHourFlowWideVO report = new StationHourFlowWideVO();
            // 当前时间点
            String timePoint = timePointList.get(i);
            Date timePointDate = DateUtil.parse(timePoint);
            // 下一个时间点
            String nextTimePoint = timePointList.get(i + 1);
            Date nextTimePointDate = DateUtil.parse(nextTimePoint);
            // 时间点
            String timePointDisplay = DateTimeUtil.getTimePoint(timePoint, timeType);
            report.setTimePoint(timePointDisplay);
            int vhc1 = 0;
            int vhc2 = 0;
            int vhc3 = 0;
            int vhc4 = 0;
            int vhc11 = 0;
            int vhc12 = 0;
            int vhc13 = 0;
            int vhc14 = 0;
            int vhc15 = 0;
            int vhc16 = 0;
            int vhc21 = 0;
            int vhc22 = 0;
            int vhc23 = 0;
            int vhc24 = 0;
            int vhc25 = 0;
            int vhc26 = 0;
            if (CollectionUtil.isNotEmpty(records)) {
                List<StationHourFlowWideVO> flowList = records.stream()
                        .filter(item -> item.getTime() != null &&  item.getTime().compareTo(timePointDate) >= 0 && item.getTime().compareTo(nextTimePointDate) < 0)
                        .collect(Collectors.toList());
                // 客车1总流量
                vhc1 = flowList.stream()
                        .mapToInt(StationHourFlowWideVO::getVhc1).sum();
                // 客车2总流量
                vhc2 = flowList.stream()
                        .mapToInt(StationHourFlowWideVO::getVhc2).sum();
                // 客车3总流量
                vhc3 = flowList.stream()
                        .mapToInt(StationHourFlowWideVO::getVhc3).sum();
                // 客车4总流量
                vhc4 = flowList.stream()
                        .mapToInt(StationHourFlowWideVO::getVhc4).sum();
                // 货车1总流量
                vhc11 = flowList.stream()
                        .mapToInt(StationHourFlowWideVO::getVhc11).sum();
                // 货车2总流量
                vhc12 = flowList.stream()
                        .mapToInt(StationHourFlowWideVO::getVhc12).sum();
                // 货车3总流量
                vhc13 = flowList.stream()
                        .mapToInt(StationHourFlowWideVO::getVhc13).sum();
                // 货车4总流量
                vhc14 = flowList.stream()
                        .mapToInt(StationHourFlowWideVO::getVhc14).sum();
                // 货车5总流量
                vhc15 = flowList.stream()
                        .mapToInt(StationHourFlowWideVO::getVhc15).sum();
                // 货车6总流量
                vhc16 = flowList.stream()
                        .mapToInt(StationHourFlowWideVO::getVhc16).sum();
                // 集装箱车1总流量
                vhc21 = flowList.stream()
                        .mapToInt(StationHourFlowWideVO::getVhc21).sum();
                // 集装箱车2总流量
                vhc22 = flowList.stream()
                        .mapToInt(StationHourFlowWideVO::getVhc22).sum();
                // 集装箱车3总流量
                vhc23 = flowList.stream()
                        .mapToInt(StationHourFlowWideVO::getVhc23).sum();
                // 集装箱车4总流量
                vhc24 = flowList.stream()
                        .mapToInt(StationHourFlowWideVO::getVhc24).sum();
                // 集装箱车5总流量
                vhc25 = flowList.stream()
                        .mapToInt(StationHourFlowWideVO::getVhc25).sum();
                // 集装箱车6总流量
                vhc26 = flowList.stream()
                        .mapToInt(StationHourFlowWideVO::getVhc26).sum();
            }
            report.setVhc1(vhc1);
            report.setVhc2(vhc2);
            report.setVhc3(vhc3);
            report.setVhc4(vhc4);
            report.setVhc11(vhc11);
            report.setVhc12(vhc12);
            report.setVhc13(vhc13);
            report.setVhc14(vhc14);
            report.setVhc15(vhc15);
            report.setVhc16(vhc16);
            report.setVhc21(vhc21);
            report.setVhc22(vhc22);
            report.setVhc23(vhc23);
            report.setVhc24(vhc24);
            report.setVhc25(vhc25);
            report.setVhc26(vhc26);
            result.add(report);
        }
        // 车型选择
        String carTypeChoose = request.getCarTypeChoose();
        // 如果对车型没有选择,直接返回
        if (StringUtils.isEmpty(carTypeChoose)) {
            return ResultUtil.success(result);
        }
        CarTypeEnum.setCarTypeStationListFLow(carTypeChoose, result);
        return ResultUtil.success(result);
    }

    @Override
    public Result<List<CrossStationStatisticReport>> getStationFlowReport(CommonRequest request) {
        // 时间类型定死按日
        request.setTimeType(ProjectConstant.DAY);
        StationHourFlowWideVO maxTimeRecord = stationHourFlowWideMapper.getMaxTimeRecord();
        // 获取这条记录时间的日期部分
        Date maxDate = DateUtil.parse(DateUtil.format(maxTimeRecord.getTime(), "yyyy-MM-dd"));
        // 开始时间是最后一次更新数据日期的0点0分0秒
        request.setStartTime(DateUtil.format(maxDate, "yyyy-MM-dd") + " 00:00:00");
        // 结束时间是最后一次更新数据日期的23点59分59秒
        request.setEndTime(DateUtil.format(maxDate, "yyyy-MM-dd") + " 23:59:59");
        // TODO-后期删除目前数据有问题直接给3月11日
        request.setStartTime("2025-03-11 00:00:00");
        request.setEndTime("2025-03-11 23:59:59");
        String isValid = QueryUtil.isValid(request);
        // 判断请求是否合法
        if (!ProjectConstant.TRUE.equals(isValid)) {
            return ResultUtil.fail(isValid);
        }
        // 收费站id-TobjTollStationInfo的map
        Map<String, TobjTollStationInfo> tollStationInfoMap = tollStationInfoService.list().stream().collect(
                Collectors.toMap(TobjTollStationInfo::getStationId, Function.identity())
        );
        // 获取所有信息
        List<StationRoadAreaInfo> stationRoadAreaInfos = stationRoadAreaInfoService.list();
        // 收费站id-收费站信息映射
        Map<String, StationRoadAreaInfo> stationRoadAreaInfoMap = stationRoadAreaInfos.stream()
                .collect(Collectors.toMap(StationRoadAreaInfo::getStationId, Function.identity()));
        // 获取道路编码-收费站id映射
        Map<String, List<String>> roadMap = stationRoadAreaInfos.stream()
                .collect(Collectors.groupingBy(StationRoadAreaInfo::getRoadCode, Collectors.mapping(StationRoadAreaInfo::getStationId, Collectors.toList())));
        // 获取区域-收费站id映射
        Map<String, List<String>> areaMap = stationRoadAreaInfos.stream()
                .collect(Collectors.groupingBy(StationRoadAreaInfo::getAreaName, Collectors.mapping(StationRoadAreaInfo::getStationId, Collectors.toList())));
        // 道路
        String road = request.getRoad();
        if (StringUtils.isNotEmpty(road)) {
            List<String> roadStationIds = roadMap.get(road);
            request.setRoadStationIds(roadStationIds);
        }
        // 区域
        String area = request.getArea();
        if (StringUtils.isNotEmpty(area)) {
            List<String> areaStationIds = areaMap.get(area);
            request.setAreaStationIds(areaStationIds);
        }
        List<StationHourFlowWideVO> records = stationHourFlowWideMapper.getStationRankReportInStation(request);
        // 收费站id-List<StationHourFlowWideVO>映射
        Map<String, List<StationHourFlowWideVO>> stationIdMap = records.stream()
                .collect(Collectors.groupingBy(StationHourFlowWideVO::getStationId));
        List<CrossStationStatisticReport> result = new ArrayList<>();
        stationIdMap.forEach((stationId, list) -> {
            CrossStationStatisticReport report = new CrossStationStatisticReport();
            report.setStationId(stationId);
            // 收费站信息实体
            StationRoadAreaInfo stationInfo = stationRoadAreaInfoMap.get(stationId);
            if (stationInfo != null) {
                report.setRoadCode(stationInfo.getRoadCode());
                report.setPileNum(stationInfo.getPileNum());
                report.setPileNumValue(stationInfo.getPileNumValue());
            }
            String stationName = tollStationInfoMap.get(stationId).getStationName();
            report.setStationName(stationName);
            // 经度
            report.setLon(tollStationInfoMap.get(stationId).getLng());
            // 纬度
            report.setLat(tollStationInfoMap.get(stationId).getLat());
            // 入口数据
            List<StationHourFlowWideVO> inFlowList = list.stream()
                    .filter(vo -> vo.getInOut() == 1)
                    .collect(Collectors.toList());
            int inVhc1 = inFlowList.stream()
                    .mapToInt(StationHourFlowWideVO::getVhc1).sum();
            report.setInVhc1(inVhc1);
            int inVhc2 = inFlowList.stream()
                    .mapToInt(StationHourFlowWideVO::getVhc2).sum();
            report.setInVhc2(inVhc2);
            int inVhc3 = inFlowList.stream()
                    .mapToInt(StationHourFlowWideVO::getVhc3).sum();
            report.setInVhc3(inVhc3);
            int inVhc4 = inFlowList.stream()
                    .mapToInt(StationHourFlowWideVO::getVhc4).sum();
            report.setInVhc4(inVhc4);
            int inVhc11 = inFlowList.stream()
                    .mapToInt(StationHourFlowWideVO::getVhc11).sum();
            report.setInVhc11(inVhc11);
            int inVhc12 = inFlowList.stream()
                    .mapToInt(StationHourFlowWideVO::getVhc12).sum();
            report.setInVhc12(inVhc12);
            int inVhc13 = inFlowList.stream()
                    .mapToInt(StationHourFlowWideVO::getVhc13).sum();
            report.setInVhc13(inVhc13);
            int inVhc14 = inFlowList.stream()
                    .mapToInt(StationHourFlowWideVO::getVhc14).sum();
            report.setInVhc14(inVhc14);
            int inVhc15 = inFlowList.stream()
                    .mapToInt(StationHourFlowWideVO::getVhc15).sum();
            report.setInVhc15(inVhc15);
            int inVhc16 = inFlowList.stream()
                    .mapToInt(StationHourFlowWideVO::getVhc16).sum();
            report.setInVhc16(inVhc16);
            // 出口数据
            List<StationHourFlowWideVO> outFlowList = list.stream()
                    .filter(vo -> vo.getInOut() == 2)
                    .collect(Collectors.toList());
            int outVhc1 = outFlowList.stream()
                    .mapToInt(StationHourFlowWideVO::getVhc1).sum();
            report.setOutVhc1(outVhc1);
            int outVhc2 = outFlowList.stream()
                    .mapToInt(StationHourFlowWideVO::getVhc2).sum();
            report.setOutVhc2(outVhc2);
            int outVhc3 = outFlowList.stream()
                    .mapToInt(StationHourFlowWideVO::getVhc3).sum();
            report.setOutVhc3(outVhc3);
            int outVhc4 = outFlowList.stream()
                    .mapToInt(StationHourFlowWideVO::getVhc4).sum();
            report.setOutVhc4(outVhc4);
            int outVhc11 = outFlowList.stream()
                    .mapToInt(StationHourFlowWideVO::getVhc11).sum();
            report.setOutVhc11(outVhc11);
            int outVhc12 = outFlowList.stream()
                    .mapToInt(StationHourFlowWideVO::getVhc12).sum();
            report.setOutVhc12(outVhc12);
            int outVhc13 = outFlowList.stream()
                    .mapToInt(StationHourFlowWideVO::getVhc13).sum();
            report.setOutVhc13(outVhc13);
            int outVhc14 = outFlowList.stream()
                    .mapToInt(StationHourFlowWideVO::getVhc14).sum();
            report.setOutVhc14(outVhc14);
            int outVhc15 = outFlowList.stream()
                    .mapToInt(StationHourFlowWideVO::getVhc15).sum();
            report.setOutVhc15(outVhc15);
            int outVhc16 = outFlowList.stream()
                    .mapToInt(StationHourFlowWideVO::getVhc16).sum();
            report.setOutVhc16(outVhc16);
            // 入口总流量
            int inFlow = inVhc1 + inVhc2 + inVhc3 + inVhc4 + inVhc11 + inVhc12 + inVhc13 + inVhc14 + inVhc15 + inVhc16;
            report.setInFlow(inFlow);
            // 出口总流量
            int outFlow = outVhc1 + outVhc2 + outVhc3 + outVhc4 + outVhc11 + outVhc12 + outVhc13 + outVhc14 + outVhc15 + outVhc16;
            report.setOutFlow(outFlow);
            // 总流量
            report.setTotalFlow(inFlow + outFlow);
            result.add(report);
        });
        // 车型选择
        String carTypeChoose = request.getCarTypeChoose();
        // 如果对车型没有选择,直接返回
        if (StringUtils.isEmpty(carTypeChoose)) {
            return ResultUtil.success(result);
        }
        CarTypeEnum.calculateAndSetObjectFlowInOutListFlow(carTypeChoose, result);
        return ResultUtil.success(result);
    }
}
